package com.hcy.app.screen.miao

import android.Manifest
import android.content.pm.PackageManager.PERMISSION_GRANTED
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.core.content.ContextCompat
import androidx.hilt.navigation.compose.hiltViewModel
import com.hcy.app.utils.HttpUtil
import kotlinx.coroutines.launch


@Composable
internal fun IDP_Info(
    idpViewModel: IDPViewModel = hiltViewModel(),
    locationPermissions: String = Manifest.permission.ACCESS_FINE_LOCATION
){
    val ctx = LocalContext.current
    val idpInfoState by idpViewModel.idpInfoState.collectAsState()
    val idpConfState by idpViewModel.idpConfState.collectAsState()
    val scope = rememberCoroutineScope()
    // 创建权限启动器
    val permissionLauncher = rememberLauncherForActivityResult(
        contract = ActivityResultContracts.RequestPermission(),
        onResult = {
        }
    )
    val snackbarHostState = remember { SnackbarHostState() }

    Column(
        modifier = Modifier.verticalScroll(rememberScrollState()).fillMaxSize().background(Color.Blue)
    ) {
        Text(text = "IMEI: ${idpInfoState.imei}")
        Text(text = "android 版本: ${idpInfoState.androidVersion}")
        Text(text = "品牌: ${idpInfoState.brand}")
        Text(text = "型号: ${idpInfoState.model}")
        Text(text = "CPU: ${idpInfoState.cpuInfo}")
        Text(text = "内存: ${idpInfoState.memory}")
        Text(text = "存储: ${idpInfoState.storageInfo}")
        Text(text = "GPS: ${idpInfoState.gps}")
        Text(text = "IP: ${idpInfoState.ip}")
        Text(text = "屏幕: ${idpInfoState.screenInfo}")
        Text(text = "软件信息: ${idpInfoState.appList.size} 条")
        Text(text = "是否越狱: ${idpInfoState.isRoot}")
        Text(text = "是否USB调试: ${idpInfoState.isUsbDebug}")
        Text(text = "是否开启VPN: ${idpInfoState.openVpn}")
        Row (
            horizontalArrangement = Arrangement.SpaceBetween,
            modifier = Modifier.fillMaxWidth()
        ) {
            Button(onClick = {
                if (ContextCompat.checkSelfPermission(ctx, locationPermissions) != PERMISSION_GRANTED) {
                    permissionLauncher.launch(locationPermissions) // 自动弹出权限请求对话框
                }else{
                    idpViewModel.updateIdpInfo(ctx)
                }
            }) { Text("刷新数据") }
            Button(onClick = {
                scope.launch {
                    val resq= HttpUtil.post(idpConfState.serAddr,idpInfoState)
                    snackbarHostState.showSnackbar("操作成功 $resq")
                }
            }) { Text("上报数据") }
        }
    }
}

